package hos.map.geometry;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import hos.map.coordinate.CoordinateType;

/**
 * <p>Title: Coordinate </p>
 * <p>Description: 几何图形 </p>
 * <p>Company: www.mapuni.com </p>
 *
 * @author : 蔡俊峰
 * @version : 1.0
 * @date : 2023-12-07 22:28
 */
public abstract class Geometry {
    private final CoordinateType type;
    protected String id;
    protected String json;

    protected Geometry(CoordinateType type) {
        this.type = type;
        this.id = System.currentTimeMillis() + "_" + this.hashCode();
    }

    /**
     * 获取坐标类型
     */
    public CoordinateType getType() {
        return type;
    }

    public String getId() {
        return id;
    }

    public String getJson() {
        return json;
    }

    public void setJson(String json) {
        this.json = json;
    }

    /**
     * 坐标转换为 百度坐标系(bd09II)
     */
    public abstract Geometry toBaiDu();

    /**
     * 坐标转换为 火星坐标系(GCJ)
     */
    public abstract Geometry toAMap();

    /**
     * 坐标转换为 地球坐标系(WGS - 84)
     */
    public abstract Geometry toWgs();

    /**
     * 获取数据
     *
     * @return id json type.getName()
     */
    public Map<String, Object> toMap() {
        Map<String, Object> data = new HashMap<>();
        data.put("id", id);
        data.put("json", json);
        data.put("type", type.getName());
        return data;
    }

    public String toJson() {
        Map<String, Object> data = toMap();
        Set<String> keySet = data.keySet();
        StringBuilder builder = new StringBuilder();
        builder.append("{");
        for (String key : keySet) {
            if (builder.length() > 1) {
                builder.append(",");
            }
            builder.append("\"").append(key)
                    .append("\"").append(":");
            Object value = data.get(key);
            if (value instanceof String) {
                builder.append("\"")
                        .append(value)
                        .append("\"");
            } else {
                builder.append(value);
            }
        }
        builder.append("}");
        return builder.toString();
    }

}
